package com.ysg.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 集合工具类
 */
public class YCollectionUtil {

    public static boolean isEmpty(Collection collection){
        if (collection != null && !collection.isEmpty()){
           return false;
        }
        return true;
    }

    public static boolean isNotEmpty(Collection collection){
        return !isEmpty(collection);
    }

    public static boolean isEmpty(Map map){
        if (map != null && !map.isEmpty()){
            return false;
        }
        return true;
    }
    public static boolean isEmpty(String[] s){
        if (s != null && s.length > 0){
            return false;
        }
        return true;
    }

    public static boolean isNotEmpty(Map collection){
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(String[] s){
        return !isEmpty(s);
    }


    /**
     * 转为分页的列表（二维数组）
     */
    public static <T> List<List<T>> getPageList(List<T> list, int pageSize) {
        List<T> datas = new ArrayList<>();
        List<List<T>> maps = new ArrayList<>();
        datas.clear();
        datas.addAll(list);
        //礼物总数量如果大于8
        if (datas.size() > pageSize) {
            int pageNum = datas.size() / pageSize;
            //每页8个，计算viewpage多少页
            for (int i = 0; i < pageNum; i++) {
                int page = (i + 1) * pageSize;
                List<T> PageItems = new ArrayList<>();
                PageItems.clear();
                if (i == 0) {//第一页
                    PageItems = datas.subList(i, page);
                } else {//次页....依次创建分页
                    PageItems = datas.subList(i * pageSize, page);
                }
                maps.add(PageItems);
            }
            int pageYU = datas.size() % pageSize;
            //如果总数整除8有余数，创建最后一页，剩余礼物添加到最后一页
            if (pageYU > 0) {
                List<T> PageItems = new ArrayList<>();
                PageItems.clear();
                PageItems = datas.subList(datas.size() - pageYU, datas.size());
                maps.add(PageItems);
            }
        } else {
            //总数不够8个，默认只创建一页
            List<T> PageItems = new ArrayList<>();
            PageItems.clear();
            PageItems = datas.subList(0, datas.size());
            maps.add(PageItems);
        }
        return maps;
    }
}
